const {
  extractAndConvertChineseNumbers,
  extractNumberFromString,
} = require("../../utils/utils");
const app = getApp();
var db = wx.cloud.database();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    foodList: [],
    showModalFlag: false,
    orderDataStr: "",
    orderDataMoney: 0,
    selectedSeatNumber: "",
  },
  orderFood: function () {
    this.checkOrderdata();
  },
  checkOrderdata: function () {
    if (this.data.foodList.length == 0) {
      return wx.showToast({
        title: "请先进行点单",
      });
    }

    if (
      this.data.foodList.filter((item) => item.price.includes("送")).length == 0
    ) {
      return wx.showToast({
        title: "请至少选择一个椒麻鸡套餐",
        icon: "none",
        duration: 2000,
      });
    }

    let orderDataStr = "";
    let orderDataMoney = 0;
    let freeDataMoney = 0;
    this.data.foodList.forEach((item) => {
      orderDataStr +=
        item.name +
        extractNumberFromString(item.price) +
        "元X" +
        item.num +
        ", ";
      freeDataMoney +=
        extractAndConvertChineseNumbers(item.price) * 2 * item.num;
      orderDataMoney += extractNumberFromString(item.price) * item.num;
    });
    orderDataStr = orderDataStr.slice(0, -2);
    this.setData({
      orderDataStr:
        orderDataStr + "\n总价为" + (orderDataMoney - freeDataMoney) + "元",
      showModalFlag: true,
    });
    console.log(this.data.orderDataStr);
  },
  handleClose: function () {
    this.setData({
      showModalFlag: false,
    });
  },

  handleConfirm: function (e) {
    const seatNumber = e.detail.seatNumber;

    this.setData({
      isModalVisible: false,
      selectedSeatNumber: seatNumber,
    });
    console.log("选定的桌子号:", seatNumber);
  },
  // 进一步处理选定的桌子号
  getData() {
    wx.showLoading({
      title: "请求中",
    });
    db.collection("addFoodList")
      .where({
        _openid: this.data.userInfo._openid,
      })
      .get()
      .then((res) => {
        wx.hideLoading();
        this.setData({
          foodList: res.data,
        });
        console.log("再次请求成功" + JSON.stringify(res));
      });
  },
  addFood: function (e) {
    const item = e.currentTarget.dataset.item;
    wx.showLoading({
      title: "加载中",
    });

    db.collection("addFoodList")
      .doc(item._id)
      .update({
        data: {
          num: item.num + 1,
        },
        success: (res) => {
          console.log("添加成功" + res.data);
          wx.hideLoading();
          this.getData();
        },
        fail: (err) => {
          console.log("添加err" + err.data);
          wx.hideLoading();
        },
      });
  },
  delectFood: function (e) {
    const item = e.currentTarget.dataset.item;
    console.log("删除" + item.num);
    wx.showLoading({
      title: "加载中",
    });
    if (item.num == 1) {
      db.collection("addFoodList")
        .doc(item._id)
        .remove({
          //   success: function (res) 中的   this.getData(); 找不到 要用      success:  (res)=>
          success: (res) => {
            console.log("删除成功" + res.data);
            wx.hideLoading();
            this.getData();
          },
          fail: (err) => {
            console.log("删除err" + err.data);
            wx.hideLoading();
            console.log(err);
          },
        });
    } else {
      console.log("走更新" + item.num);
      db.collection("addFoodList")
        .doc(item._id)
        .update({
          data: {
            num: item.num - 1,
          },
          success: (res) => {
            wx.hideLoading();
            this.getData();
          },
          fail: (err) => {
            wx.hideLoading();
            console.log(err);
          },
        });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      userInfo: app.globalData.userInfo,
    });
    if (this.data.userInfo != null) {
      this.getData();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},
});
